package mobi.hami.huapin.service.impl;

import mobi.hami.huapin.dao.FoodSortMapper;
import mobi.hami.huapin.dao.ProductMapper;
import mobi.hami.huapin.dao.UserCouponMapper;
import mobi.hami.huapin.dto.UserCouponDTO;
import mobi.hami.huapin.model.UserCoupon;
import mobi.hami.huapin.service.UserCouponService;
import org.aspectj.weaver.ast.Var;
import org.omg.CORBA.Environment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Create by 小峰 on 2018/8/24.
 */
@Service
public class UserCouponServiceImpl implements UserCouponService {
    @Autowired
    private UserCouponMapper userCouponMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private FoodSortMapper foodSortMapper;
    @Override
    public List<UserCouponDTO> getCouponByUserId(Integer userId) {
        UserCoupon userCoupon = new UserCoupon();
        userCoupon.setUser_id(userId);
        userCoupon.setIs_valid(3);
        List<UserCouponDTO> userCouponList = userCouponMapper.selectListByModel(userCoupon);
        userCouponList.forEach(m -> {
            if(m.getGoods_id()!=null && m.getGoods_id()>0)
            {
                m.setGoodsName(productMapper.selectByPrimaryKey(m.getGoods_id().longValue()).getName());
            }
            if(m.getSort_id()!=null && m.getSort_id()>0)
            {
                m.setSortName(foodSortMapper.selectByPrimaryKey(m.getSort_id()).getSortname());
            }
        });
        return userCouponList;
    }

    @Override
    public int update(UserCoupon userCoupon) {
        return userCouponMapper.updateByPrimaryKeySelective(userCoupon);
    }
}
